﻿@model CurrencySearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Configuration.Currencies").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Currencies");
}

<form asp-controller="Currency" asp-action="List" method="post">
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.Configuration.Currencies")
        </h1>
        <div class="pull-right">
            <button type="submit" name="save" class="btn btn-primary">
                <i class="fa fa-floppy-o"></i>
                @T("Admin.Common.Save")
            </button>
            <a asp-controller="Currency" asp-action="List" asp-route-liveRates="true" class="btn bg-green">
                <i class="fa fa-tasks"></i>
                @T("Admin.Configuration.Currencies.GetLiveRates")
            </a>
            <a asp-action="Create" class="btn bg-blue">
                <i class="fa fa-plus-square"></i>
                @T("Admin.Common.AddNew")
            </a>
            @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.CurrencyListButtons })
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="ExchangeRateProviderModel.ExchangeRateProvider" />
                            </div>
                            <div class="col-md-9">
                                <nop-select asp-for-name="ExchangeRateProviderModel.ExchangeRateProvider" asp-items="Model.ExchangeRateProviderModel.ExchangeRateProviders" />
                                <span asp-validation-for="ExchangeRateProviderModel.ExchangeRateProvider"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="ExchangeRateProviderModel.AutoUpdateEnabled" />
                            </div>
                            <div class="col-md-9">
                                <nop-editor asp-for="ExchangeRateProviderModel.AutoUpdateEnabled" />
                                <span asp-validation-for="ExchangeRateProviderModel.AutoUpdateEnabled"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "currencies-grid",
                            UrlRead = new DataUrl("ListGrid", "Currency", null),
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(CurrencyModel.Name))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.Name").Text,
                                    Width = "200"
                                },
                                new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.CurrencyCode").Text,
                                    Width = "150"
                                },
                                new ColumnProperty(nameof(CurrencyModel.Rate))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.Rate").Text,
                                    Width = "100"
                                },
                                new ColumnProperty(nameof(CurrencyModel.IsPrimaryExchangeRateCurrency))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.IsPrimaryExchangeRateCurrency").Text,
                                    Width = "250",
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                },
                                new ColumnProperty(nameof(CurrencyModel.Id))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency").Text,
                                    Width = "200",
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Render = new RenderButtonCustom(NopButtonClassDefaults.Success, T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency").Text)
                                    {
                                        OnClickFunctionName =  "markAsPrimaryExchangeRateCurrency"
                                    }
                                },
                                new ColumnProperty(nameof(CurrencyModel.IsPrimaryStoreCurrency))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.IsPrimaryStoreCurrency").Text,
                                    Width = "200",
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                },
                                new ColumnProperty(nameof(CurrencyModel.Id))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency").Text,
                                    Width = "200",
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Render = new RenderButtonCustom(NopButtonClassDefaults.Olive, T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency").Text)
                                    {
                                        OnClickFunctionName = "markAsPrimaryStoreCurrency"
                                    }
                                },
                                new ColumnProperty(nameof(CurrencyModel.Published))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.Published").Text,
                                    Width = "50",
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                },
                                new ColumnProperty(nameof(CurrencyModel.DisplayOrder))
                                {
                                    Title = T("Admin.Configuration.Currencies.Fields.DisplayOrder").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.CenterAll
                                },
                                new ColumnProperty(nameof(CurrencyModel.Id))
                                {
                                    Title = T("Admin.Common.Edit").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Render = new RenderButtonEdit(new DataUrl("Edit"))
                                }
                            }
                        })

                        
                    <button type="submit" class="btn btn-default" id="btnMarkPERC" style="display:none" />
                    <button type="submit" class="btn btn-default" id="btnMarkPSC" style="display:none" />

                    <script>
                        var selectedId;

                        $(document).ready(function () {

                            $("#btnMarkPSC-action-confirmation-submit-button").bind("click", function () {
                                var postData = {
                                    id: selectedId
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("MarkAsPrimaryStoreCurrency", "Currency"))",
                                    data: postData,
                                    success: function (data, textStatus, jqXHR) {
                                        updateTable('#currencies-grid');
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $("#currencyAlert").click();
                                    }
                                });
                                $('#btnMarkPSC-action-confirmation').modal('toggle');
                            });

                            $("#btnMarkPERC-action-confirmation-submit-button").bind("click", function () {
                                var postData = {
                                    id: selectedId
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("MarkAsPrimaryExchangeRateCurrency", "Currency"))",
                                    data: postData,
                                    success: function (data, textStatus, jqXHR) {
                                        updateTable('#currencies-grid');
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $("#currencyAlert").click();
                                    }
                                });
                                $('#btnMarkPERC-action-confirmation').modal('toggle');
                            });

                        });

                        function markAsPrimaryExchangeRateCurrency(id) {
                            selectedId = id;
                            $("#btnMarkPERC").click();
                        };

                        function markAsPrimaryStoreCurrency(id) {
                            selectedId = id;
                            $("#btnMarkPSC").click();
                        };
                    </script>
                    </div>
                </div>
                @if (Model.ExchangeRateProviderModel.ExchangeRates?.Any() ?? false)
                {
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            @T("Admin.Configuration.Currencies.LiveRates")
                        </div>
                        <div class="panel-body">

                            <a onclick="applyCurrencyRate()" class="btn bg-olive">@T("Admin.Configuration.Currencies.ApplyRate.All")</a>
                            @await Html.PartialAsync("Table", new DataTablesModel
                            {
                                Name = "rate-grid",
                                Data = "currencyrates_source",
                                ServerSide = false,
                                Paging = false,
                                Length = Model.PageSize,
                                LengthMenu = Model.AvailablePageSizes,
                                ColumnCollection = new List<ColumnProperty>
                                {
                                    new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.CurrencyCode").Text,
                                        Width = "300"
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Rate))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.Rate").Text,
                                        Width = "300"
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                    {
                                        Title = T("Admin.Configuration.Currencies.ApplyRate").Text,
                                        Width = "200",
                                        ClassName = NopColumnClassDefaults.Button,
                                        Render = new RenderCustom("renderColumnApplyRate")
                                    }
                                }
                            })
                            <script>
                                function renderColumnApplyRate(data, type, row, meta) {
                                    return '<a onclick="applyCurrencyRate(\'' +
                                        row.Rate +
                                        '\',\'' +
                                        row.CurrencyCode +
                                        '\')" class="btn bg-olive">@T("Admin.Configuration.Currencies.ApplyRate")</a>';
                                }

                                function applyCurrencyRate(rate, currencyCode) {
                                    var postData = {
                                        rateModels: (rate && currencyCode) ? [{ CurrencyCode: currencyCode, Rate: rate }] : currencyrates_source
                                    };
                                    addAntiForgeryToken(postData);
                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("ApplyRates"))",
                                        data: postData,
                                        success: function (data, textStatus, jqXHR) {
                                            updateTable('#currencies-grid');
                                        },
                                        error: function (jqXHR, textStatus, errorThrown) {
                                            $("#currencyAlert").click();
                                        }
                                    });
                                }

                                //local datasource
                                var currencyrates_source = [
                                    @for (var i = 0; i < Model.ExchangeRateProviderModel.ExchangeRates.Count; i++)
                                    {
                                        var rate = Model.ExchangeRateProviderModel.ExchangeRates[i];
                                        <text>
                                            {
                                                CurrencyCode:
                                                    "@(Html.Raw(JavaScriptEncoder.Default.Encode(rate.CurrencyCode)))",
                                                Rate:
                                                    "@(Html.Raw(JavaScriptEncoder.Default.Encode(rate.Rate.ToString())))"
                                            }
                                        </text>
                                        if (i != Model.ExchangeRateProviderModel.ExchangeRates.Count - 1)
                                        {
                                            <text>,</text>
                                        }
                                    }
                                ];
                            </script>
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
</form>
<nop-action-confirmation asp-button-id="btnMarkPERC" asp-additional-confirm="Admin.Configuration.Currencies.AdditionalConfirm" />
<nop-action-confirmation asp-button-id="btnMarkPSC" asp-additional-confirm="Admin.Configuration.Currencies.AdditionalConfirm" />
<nop-alert asp-alert-id="currencyAlert" asp-alert-message="@T("Admin.Configuration.Currencies.Alert.Error")" />